package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.media.dto.ChannelDto;
import com.heima.media.entity.WmChannel;
import com.heima.media.mapper.WmChannelMapper;
import com.heima.media.service.IWmChannelService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 频道信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since
 */
@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements IWmChannelService{
    //至少在当前业务逻辑层定义方法：有当前模块基本CRUD方法

   /* @Autowired
    private WmChannelMapper wmChannelMapper;

    @Override
    public WmChannel getById(Integer id) {
        return wmChannelMapper.selectById(id);
    }*/


    /**
     * 根据频道名称 以及 状态 查询频道列表
     * sql:select * from wm_channel wc where wc.name like '%java%' and wc.status = 1 limit 0,5
     *
     * @param dto 查询条件
     * @return 分页结果
     */
    @Override
    public ResponseResult listByName(ChannelDto dto) {
        //1.封装查询条件
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        //1.1 sql=select * from wm_channel wc where wc.name like '%java%'
        if (StringUtils.isNotBlank(dto.getName())) {
            queryWrapper.like(WmChannel::getName, dto.getName());
        }
        //1.2sql+=and wc.status = 1
        if (dto.getStatus() != null) {
            queryWrapper.eq(WmChannel::getStatus, dto.getStatus());
        }

        //2.封装分页信息对象Page  当前页，页大小
        IPage<WmChannel> page = new Page<>(dto.getPage(), dto.getSize());

        //3.调用继承来的业务逻辑层分页条件查询方法 当前页数据，总记录数等信息封装到IPA个其他方法中
        IPage<WmChannel> page1 = this.page(page, queryWrapper);

        //4.封装分页结果 page 结果不符合api接口中要求
        PageResponseResult result = new PageResponseResult<List<WmChannel>>(dto.getPage(), dto.getSize(), page.getTotal(), page.getRecords());
        return result;
    }
}
